import sqlite3
from flask import Flask
from flask import request, redirect, url_for
from flask import render_template

conn = sqlite3.connect('./aa.db',check_same_thread = False)
c = conn.cursor()

app = Flask(__name__)

try:
    c.execute('CREATE TABLE  feed(id integer primary key autoincrement, con text)')
except sqlite3.OperationalError:
    pass

@app.route('/', methods=['POST', 'GET'])
def index():
	if request.method == 'GET':
		feed_list = list()

		for feed in c.execute('SELECT * FROM feed'):
			temp_dict = {'id': feed[0], 'content': feed[1]} 
			feed_list.append(temp_dict)
			
		return render_template('index.html', feed_list=feed_list)
		
	if request.method == 'POST':
		con = request.form['content']
		insert_sql = "INSERT INTO feed VALUES (null, '%s')" % con
		c.execute(insert_sql)
		conn.commit()
		return redirect('/')


@app.route('/remove/<feed_id>')
def remove(feed_id):
	delete_sql = 'delete from feed where id = %s;' % feed_id
	c.execute(delete_sql)
	conn.commit()
	return redirect('/')
		
if __name__ == '__main__':
    app.run(debug=True)
